package com.dense.kuiniu.shelfcard.setting;

import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.dense.kuiniu.shelfcard.R;
import com.dense.kuiniu.shelfcard.base.BaseFragment;
import com.dense.kuiniu.shelfcard.base.SpKey;
import com.dense.kuiniu.shelfcard.base.Url;
import com.dense.kuiniu.shelfcard.entity.Binding;
import com.dense.kuiniu.shelfcard.entity.Device;
import com.dense.kuiniu.shelfcard.entity.Library;
import com.dense.kuiniu.shelfcard.utils.search.SearchUtils;
import com.rxjava.rxlife.RxLife;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import rxhttp.RxHttp;

/**
 * @author Administrator
 * @date 2020/8/17
 */
public class SettingDelegate extends BaseFragment {
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.et_address)
    EditText mEtAddress;
    @BindView(R.id.spinner)
    Spinner mSpinner;
    private ArrayAdapter<Library> mArrayAdapter;

    public static SettingDelegate getInstance() {
        return new SettingDelegate();
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.delegate_setting;
    }

    @OnClick({R.id.tv_return, R.id.tv_binding_modify, R.id.tv_address_modify})
    public void onClick(View view) {
        super.onClick(view);
    }

    @Override
    protected void onClick2(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_binding_modify:
                modifyBinding();
                break;
            case R.id.tv_address_modify:
                modifyAddress();
                break;
            case R.id.tv_return:
                pop();
                break;
        }
    }

    @Override
    protected void init() {
        mTvTitle.setText("设置");
        String url = SPUtils.getInstance().getString(SpKey.KEY_BASE_URL);
        if (url.isEmpty()) {
            url = Url.baseUrl;
        }
        mEtAddress.setText(url);
        queryLibrary();
    }

    private void queryLibrary() {
        RxHttp.get(Url.getBindingLibraryInfo)
                .add("deviceId", DeviceUtils.getAndroidID())
                .add("deviceType", "Android")
                .asResponse(Binding.class)
                .to(RxLife.toMain(this))  //感知生命周期，并在主线程回调
                .subscribe(binding -> {
                    List<Library> libraries = binding.getLibraryList();
                    if (libraries != null) {
                        if (mSpinner != null && getActivity() != null) {
                            mSpinner.setAdapter(new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, libraries));
                            int index = SearchUtils.search(binding.getLibraryCode(), libraries);
                            if (index != -1) {
                                mSpinner.setSelection(index);
                            }
                        }
                    }
                }, throwable -> {
                    ToastUtils.showShort(throwable.getMessage());
                });
    }

    private void binding(String libraryName, String libraryCode) {
        RxHttp.postJson(Url.bindingLibrary)
                .add("deviceId", DeviceUtils.getAndroidID())
                .add("deviceType", "Android")
                .add("deviceName", "办卡机")
                .add("libraryCode", libraryCode)
                .asResponse(Device.class)
                .to(RxLife.toMain(this))
                .subscribe(device -> {
                    SPUtils.getInstance().put(SpKey.KEY_BINDING_LIBRARY, device.getLibraryCode());
                    SPUtils.getInstance().put(SpKey.KEY_BINDING_LIBRARY_NAME, libraryName);
                    SPUtils.getInstance().put(SpKey.KEY_DEVICE_CODE, device.getDeviceNum());
                    ToastUtils.showShort("绑定成功!");
                }, throwable -> {
                    ToastUtils.showShort(throwable.getMessage());
                });
    }

    private void modifyAddress() {
        String address = mEtAddress.getText().toString();
        if (address.isEmpty()) {
            ToastUtils.showShort("地址不能为空");
        } else {
            SPUtils.getInstance().put(SpKey.KEY_BASE_URL, address);
            Url.baseUrl = address;
            ToastUtils.showShort("设置成功");
        }
    }

    private void modifyBinding() {
        Library library = (Library) mSpinner.getSelectedItem();
        if (library != null) {
            binding(library.getName(), library.getLibraryCode());
        }
    }

}